import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demotest/router/router.dart';
import 'package:flutter_demotest/store/user.dart';
import 'package:flutter_demotest/utils/network_manager.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hive_flutter/adapters.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  await UserStore.init();
  await NetworkManager().init();
  runApp(const MyApp());

  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark,
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void dispose() {
    NetworkManager().dispose();  // 释放网络监听资源
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: FlutterSmartDialog.init(),
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        bottomNavigationBarTheme: const BottomNavigationBarThemeData(
          selectedIconTheme: IconThemeData(opacity: 1.0),
          unselectedIconTheme: IconThemeData(opacity: 0.7),
        ),
        splashFactory: NoSplash.splashFactory,
      ),
      key: navigatorKey,
      onGenerateRoute: AppRouter.generateRoute,
      initialRoute: '/',
    );
  }
}
